/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.swirrel.installer;

import java.awt.Component;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;
import javax.swing.event.CaretListener;
import javax.swing.text.JTextComponent;
import org.swirrel.annotation.CaretUpdate;
import org.swirrel.listener.SwirrelCaretListener;
import org.swirrel.util.MethodCall;

/**
 *
 * @author Gronau
 */
public class CaretListenerInstaller extends AbstractListenerInstaller implements SwirrelListenerInstaller {
    public CaretListenerInstaller() {
        super(Arrays.<Class<? extends Annotation>>asList(CaretUpdate.class),
                Arrays.<Class<?>>asList(JTextComponent.class));
    }

    public boolean isListenerAlreadySet(Object obj) {
       CaretListener[] caretListeners = ((JTextComponent) obj).getCaretListeners();
        for (CaretListener cl : caretListeners) {
            if (cl instanceof SwirrelCaretListener) {
                return true;
            }
        }
        return false;
    }

    public void registerListener(Field field, Component comp, java.util.List<MethodCall> methodCalls) throws IllegalAccessException {
        Object obj = field.get(comp);
        CaretListener cl = new SwirrelCaretListener(methodCalls.get(0));
         ((JTextComponent) obj).addCaretListener(cl);
    }
}
